﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using HandyControl.Controls;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WpfDemo.Common;
using WpfDemo.IRepository.Sys;
using WpfDemo.Models.Migrations;
using WpfDemo.Repository.Sys;

namespace WpfDemo.ViewModel
{
    public partial  class LoginViewModel : ObservableObject
    {
        IUserRepository _userRepository;
        IAuthRepository _authrepository;
        public LoginViewModel(IUserRepository userRepository, IAuthRepository authRepository)
        {
            _userRepository=userRepository;
            _authrepository =authRepository;


        }

        [ObservableProperty]
        private string? userName="admin";

        [ObservableProperty]
        private string? password = "123456";

        [RelayCommand]
        private void loginCmd(object o)
        {
            var values = (object[])o;
            var psdControl = (PasswordBox)values[1];
            string psdStr = psdControl.Password;
            if (string.IsNullOrEmpty(userName))
            {                
                WeakReferenceMessenger.Default.Send<string, string>("登录名不能为空", "UserNameErrorToken");                
                return;
            }
            if (string.IsNullOrEmpty(psdStr))
            {
                psdControl.IsError = true;
                psdControl.ErrorStr = "密码不能为空";
                return;
            }
            var pwd = DESEncrypt.Encrypt(psdStr);
            //var pwd2 = DESEncrypt.Decrypt(pwd);
            var UserEnt = _userRepository.QuerySingle(m => m.F_UserName == userName && m.F_Password == pwd);
            if(UserEnt==null)
            {
                psdControl.IsError = true;
                psdControl.ErrorStr = "用户名或密码错误";
                return;
            }
            if(!string.IsNullOrEmpty(UserEnt.F_IsDisabled)&& UserEnt.F_IsDisabled =="1")
            {
                psdControl.IsError = true;
                psdControl.ErrorStr = "账号已禁用";
                return;
            }
            Utils.CurrentUser = UserEnt;
            bool isAdmin = UserEnt.F_IsAdmin == null ? false : (UserEnt.F_IsAdmin==0?false:true);
            Utils.IsAdmin=isAdmin;

            var buttons = _authrepository.Query().Include(m=>m.FModuleEntity).Include(m=>m.FButtonEntity).Where(m => m.F_RoleId == UserEnt.F_RoleId).ToList();
            Utils.Auths=buttons;

            if(!isAdmin && (buttons ==null || buttons.Count()==0))
            {
                psdControl.IsError = true;
                psdControl.ErrorStr = "账号未分配权限";
                return;
            }
            View.MainWindow mainView = App.ServiceProvider.GetService<View.MainWindow>();      
            

            var window = App.ServiceProvider.GetService<View.LoginView>();//GetRequiredService
            window.Close();
            mainView.ShowDialog();
            Application.Current.Shutdown();
        }
    }
}
